//在命令行通过一些指令来在一个“画布”上画“\/-|+”，即横竖斜线和交叉。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
struct Canvas
{
    int r;
    int c;
    char **buf;
    Canvas()
    {
        r = -1;
        c = -1;
    }
    Canvas(int r_, int c_)
    {
        ReInit(r_, c_);
    }
    void ReInit(int r_, int c_)
    {
        char **newBuf;
        int i, j;
        newBuf = (char**)malloc(sizeof(char*) * r_);
        for(i = 0; i < r_; i ++)
            newBuf[i] = (char*)malloc(sizeof(char) * (c_ + 1));
        for(i = 0; i < r_; i ++)
            for(j = 0; j < c_; j ++)
                newBuf[i][j] = '*';
        for(i = 0; i < r && i < r_; i ++)
            for(j = 0; j < c && j < c_; j ++)
                newBuf[i][j] = buf[i][j];
        Free();
        buf = newBuf;
        r = r_;
        c = c_;
    }
    void Free()
    {
        int i;
        if(r == -1)
            return;
        for(i = 0; i < r; i ++)
            free(buf[i]);
        free(buf);
        r = -1;
        c = -1;
        return;
    }
    void Draw()
    {
        int i, j;
        for(i = 0; i < r; i ++)
        {
            printf("%d", r - i - 1);
            for(j = 0; j < c; j ++)
                printf(" %c", buf[r - i - 1][j]);
            printf("\n");
        }
        printf(" ");
        for(j = 0; j < c; j ++)
            printf(" %d", j);
        printf("\n");
    }
    void Add(char rc, int site)
    {
        int r_ = r, c_ = c;
        int i, j;
        if(rc == 'r')
            r_ ++;
        else
            c_ ++;
        ReInit(r_, c_);
        if(rc == 'r')
        {
            for(j = c_ - 1; j >= 0; j --)
            {
                for(i = r_ - 1; i > site; i --)
                    buf[i][j] = buf[i - 1][j];
                buf[i][j] = '*';
            }
        }
        else
        {
            for(i = r_ - 1; i >= 0; i --)
            {
                for(j = c_ - 1; j > site; j --)
                    buf[i][j] = buf[i][j - 1];
                buf[i][j] = '*';
            }
        }
    }
    void Delete(char rc, int site)
    {
        int r_ = r, c_ = c;
        int i, j;
        if(rc == 'r')
            r_ --;
        else
            c_ --;
        if(rc == 'r')
        {
            for(j = c - 1; j >= 0; j --)
            {
                for(i = site; i < r - 1; i ++)
                    buf[i][j] = buf[i + 1][j];
            }
        }
        else
        {
            for(i = r - 1; i >= 0; i --)
            {
                for(j = site; j < c - 1; j ++)
                    buf[i][j] = buf[i][j + 1];
            }
        }
        ReInit(r_, c_);
    }
};
int Quit(Canvas *ca)
{
    ca->Free();
    return 0;
}
int Help()
{
    return 0;
}
char JudgeDrawValid(int start_row, int start_column, int end_row, int end_column, Canvas *ca)
{
    if(start_row == end_row)
        return '-';
    else if(start_column == end_column)
        return '|';
    else if(end_column - start_column == end_row - start_row)
        return '/';
    else if(end_column - start_column == start_row - end_row)
        return '\\';
    // TODO judge the range of the four numbers with ca->r and ca->c
//    if()
//    {
//
//    }
    return '#';
}
void Swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}
int Write(Canvas *ca)
{
    int start_row, start_column, end_row, end_column;
    char toDraw;
    int i, j;
    scanf("%d%d%d%d", &start_row, &start_column, &end_row, &end_column);
    toDraw = JudgeDrawValid(start_row, start_column, end_row, end_column, ca);
    if(toDraw == '#')
    {
        printf("xxx");
        return 1;
    }

    if(start_row > end_row)
    {
        Swap(&start_row, &end_row);
        Swap(&start_column, &end_column);
    }
    else if(start_row == end_row)
    {
        if(start_column > end_column)
        {
            Swap(&start_column, &end_column);
        }
    }
    i = start_row, j = start_column;
    while(true)
    {
        if(ca->buf[i][j] != toDraw && ca->buf[i][j] != '*')
            ca->buf[i][j] = '+';
        else
            ca->buf[i][j] = toDraw;
        if(toDraw == '-')
        {
            j ++;
            if(j > end_column)
                break;
        }
        else if(toDraw == '|')
        {
            i ++;
            if(i > end_row)
                break;
        }
        else if(toDraw == '\\')
        {
            i ++;
            j --;
            if(i > end_row)
                break;
        }
        else
        {
            i ++;
            j ++;
            if(i > end_row)
                break;
        }
    }
    return 0;
}
void Erase(Canvas *ca)
{
    int r_, c_;
    scanf("%d%d", &r_, &c_);
    // TODO Judge Valid
    ca->buf[r_][c_] = '*';
}
void Resize(Canvas *ca)
{
    int r_, c_;
    scanf("%d%d", &r_, &c_);
    ca->ReInit(r_, c_);
}
void Add(Canvas *ca)
{
    //TODO Judge valid
    char co[10];
    int site;
    scanf("%s%d", co, &site);
    ca->Add(co[0], site);
}
void Delete(Canvas *ca)
{
    //TODO Judge valid
    char co[10];
    int site;
    scanf("%s%d", co, &site);
    ca->Delete(co[0], site);
}
void Save(Canvas *ca)
{
    char filename[200];
    int i;
    scanf("%s", filename);
    FILE *f = fopen(filename, "wb");
    fprintf(f, "%d %d\n", ca->r, ca->c);
    for(i = 0; i < ca->r; i ++)
        fprintf(f, "%s\n", ca->buf[i]);
    fclose(f);
}
void Load(Canvas *ca)
{
    char filename[200];
    int i, r_, c_;
    scanf("%s", filename);
    FILE *f = fopen(filename, "rb");
    fscanf(f, "%d %d", &r_, &c_);
    ca->ReInit(r_, c_);
    for(i = 0; i < ca->r; i ++)
        fscanf(f, "%s", ca->buf[i]);
    fclose(f);
}
int main(int argc, char *argv[])
{
    char co[20];
    int r_, c_;
    Canvas ca;
    if(argc < 3)
    {
        ca.ReInit(10, 10);
    }
    else
    {
        // Judge the input.
        sscanf(argv[1], "%d", &r_);
        sscanf(argv[2], "%d", &c_);
        ca.ReInit(r_, c_);
    }
    ca.Draw();
    while(scanf("%s", co) != EOF)
    {
        if(co[0] == 'q')
        {
            Quit(&ca);
            return 0;
        }
        else if(co[0] == 'h')
        {
            Help();
        }
        else if(co[0] == 'w')
        {
            Write(&ca);
        }
        else if(co[0] == 'e')
        {
            Erase(&ca);
        }
        else if(co[0] == 'r')
        {
            Resize(&ca);
        }
        else if(co[0] == 'a')
        {
            Add(&ca);
        }
        else if(co[0] == 'd')
        {
            Delete(&ca);
        }
        else if(co[0] == 's')
        {
            Save(&ca);
        }
        else if(co[0] == 'l')
        {
            Load(&ca);
        }
        ca.Draw();
    }
    return 0;
}
